#开闭运算
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img=cv.imread('F:\\11\\13.png',1)
kernel=np.ones((5,5),np.uint8)
op=cv.MORPH_OPEN
cl=cv.MORPH_CLOSE
top=cv.MORPH_TOPHAT
black=cv.MORPH_BLACKHAT
def open(op):#开运算
    img1=cv.morphologyEx(img,op,kernel)
    return img1
def close(cl):#闭运算
    img1=cv.morphologyEx(img,cl,kernel)
    return img1
def show(img,name):#利用matplotlib显示图像
    plt.imshow(img[:, :, ::-1])
    plt.title(name), plt.xticks([]), plt.yticks([])
    plt.show()
def tophat(tophat):#礼帽运算
    img1=cv.morphologyEx(img,tophat,kernel)
    return img1
def blackhat(blackhat):#黑帽运算
    img1=cv.morphologyEx(img,blackhat,kernel)
    return img1
if __name__=="__main__":
    show(open(op),'open')
    show(close(cl),'close')
    show(tophat(top),'tophat')
    show(blackhat(black),'blackhat')